<?php


    class videoEngine{


        function exportVideo($fullPathToVideo, $optionArray = array(), $sliceOptions = "", $partnumber){


            if(is_array($sliceOptions)){
                $timeline = " -ss ".$sliceOptions['vStart']." -t ".$sliceOptions['vDuration']." ";
            }

            $exec = "ffmpeg -i ".$fullPathToVideo. " ".     //Orginal Video
                $timeline.
                "-vcodec libx264 ".                         //Videocodec
                "-level ".$optionArray['level'] . " ".      //Quaili Level
                "-b ".$optionArray['bitrate'] . " ".        //Bitrate
                "-r 25 ".                                   //Framerate
                "-bt ".$optionArray['bitrate'] . " ".       //Set video bitrate tolerance (in bits, default 4000k). Has a minimum value of: (target_bitrate/target_framerate). In 1-pass mode, bitrate tolerance specifies how far ratecontrol is willing to deviate from the target average bitrate value. This is not related to min/max bitrate. Lowering tolerance too much has an adverse effect on quality
                "-s ".$optionArray['size']. " ".            //Videogröße
                "-y ".                                      //overwrite output files
                "-coder 1 ".
                "-flags +loop ".
                "-cmp +chroma ".
                "-partitions +parti8x8+parti4x4+partp8x8+partb8x8 ".
                "-me_method full ".                         //Set motion estimation method to method. Available methods are (from lowest to best quality):
                "-subq 7 ".
                "-me_range 23 ".
                "-g 250 ".                                  //Set the group of pictures size.
                "-keyint_min 25 ".
                "-sc_threshold 40 ".
                "-i_qfactor 0.71 ".
                "-b_strategy 1 ".
                "-qcomp 0.6 ".
                "-qmin 10 ".
                "-qmax 51. ".
                "-qdiff 4. ".                                //maximum difference between the quantizer scales (VBR)
                "-bf 16 ".                                   //Use ’frames’ B-frames (supported for MPEG-1, MPEG-2 and MPEG-4).
                "-refs 6 ".
                "-directpred 2 ".
                "-trellis 1 ".
                "-flags2 +bpyramid+wpred+mixed_refs+dct8x8+fastpskip ".
                "-f ".$optionArray['vcodec']." ".                                  //format
                "-acodec libfaac ".
                "-ab 56000 ".
                "-ac 2 ".
                "-ar 48000 ".                                   //Audio Bitrate
                $optionArray['tragetdir']."/OUTPUT_".$partnumber.".mp4";

            echo $exec;
            exec($exec);

        }

        //Videos auf eine einheitliche größe bringen
        function paddingVideo($fullPathToVideo, $optionArray = array()){

            exec("mencoder
                    -y
                    -forceidx
                    -of lavf
                    -oac pcm
                    -ovc lavc
                    -vf scale=".$optionArray['xWidht'].":".$optionArray['yHeight'].",
                    expand=".$optionArray['expandWidht'].":".$optionArray['expandHeight']."
                    -lavcopts aspect=16/9 ".$fullPathToVideo."
                    -o new1.mkv");
        }



        function mergeVideo($parts){
            //Mencoder join 2 Videos gute quali
            $mergeStr = "";
            for($i = 1; $i <= $parts; $i++){
                $mergeStr .= "/home/www/workspaces/mklein/oncut/core/video/backend/hd_1280_720/OUTPUT_".$i.".mp4 ";
            }

            $exec = "mencoder -ovc lavc -lavcopts aglobal=1:vglobal=1:vcodec=mpeg4:vbitrate=4570000:acodec=libfaac -oac pcm ".$mergeStr." -o /home/www/workspaces/mklein/oncut/core/video/backend/hd_1280_720/complete.mp4";

            exec($exec);
            echo $exec;

        }


    }